package knicker.api.servlet.auth;

import java.util.Calendar;

import net.jeremybrooks.knicker.*;
import net.jeremybrooks.knicker.dto.*;

public class Authenticator {
	
	private static String api_Key					= null;
	private static String user 						= null;
	private static String passwd					= null;
	private static AuthenticationToken authToken	= null;
	private static long loginTime					= 0;
	private static long resetInMillis				= 0;
	
	public Authenticator(String apiKey, String user, String passwd) throws KnickerException{
;
			try {
				System.setProperty("WORDNIK_API_KEY", apiKey);
				this.authToken = AccountApi.authenticate(user, passwd);
				this.resetInMillis = AccountApi.apiTokenStatus().getResetsInMillis();
				this.loginTime = Calendar.getInstance().getTimeInMillis();
				this.api_Key = apiKey;
				this.user = user;
				this.passwd = passwd;
			} catch (KnickerException e) {
				throw new KnickerException(resetAuthenticator());
			}
	}
	public static long getResetInMillis() {
		return resetInMillis;
	}
	public static String getApiKey() {
		return api_Key;
	}
	public static String getUser() {
		return user;
	}
	public static String getPasswd() {
		return passwd;
	}
	public static AuthenticationToken getAuthToken() {
		return authToken;
	}
	
	public static void auth(String apiKey, String user, String passwd) throws KnickerException{
		new Authenticator(apiKey, user, passwd);
	}
	public String resetAuthenticator() {
		this.api_Key = null;
		this.user = null;
		this.passwd = null;
		this.authToken = null;
		this.loginTime = 0;
		return "!!! RESET ALL !!! WRONG CREDENTIALS !!!";
	}
	public static long getTokenStatus() {
		return loginTime;
	}
}
